package com.pj.utils;

import com.pj.redis.RedisUtils;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;

/**
 * @author: szy
 * @program: pj-novel
 * @description: 请求工具类
 * @create: 2022-11-08 11:51
 **/

public class HttpUtil {

    private static final RestTemplate restTemplate = RedisUtils.getInstance("utf-8");
    public static String getByHttpClientWithChrome(String url) {
        try {
            HttpHeaders headers = new HttpHeaders();
            headers.add("user-agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36");
            HttpEntity<String> requestEntity = new HttpEntity<>(null, headers);
            ResponseEntity<String> forEntity = restTemplate.exchange(url.toString(), HttpMethod.GET, requestEntity, String.class);

            if (forEntity.getStatusCode() == HttpStatus.OK) {
                return forEntity.getBody();
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
